﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AirlineServiceFacade ;
using AirlineCommon ;
namespace AirlineSystemApp
{
    public partial class frmCancelReservation : Form
    {
        private IAirlineServices airlineSv;
        private AirlineFactory factory;
        private TravelClass travelclass;
        private PassengerCommon passenger;
        
        public frmCancelReservation()
        {
            InitializeComponent();
        }

        private void cmdGetDetails_Click(object sender, EventArgs e)
        {
            airlineSv =new AirlineServices ();
            Ticket ticket=new Ticket ();
            string PNRNo = txtPnrNo.Text;
            string Class = ticket.GetPassengerClassByPNRNo(PNRNo);
            if (Class == string.Empty)
            {
                MessageBox.Show("This PNR Number is not exists");
                ResetData();
                return;
            }
            if (Class.ToLower().Trim() == "first")
            {
                factory = new FirstFactory();
            }
            else if (Class.ToLower().Trim() == "business")

            {
                factory = new BusinessFactory();
            }
            else
            {
                factory = new EconomyFactory();
            }
            travelclass = airlineSv.CreateTravelClass(factory);
            passenger = airlineSv.CreatePassenger(factory);
            passenger = passenger.GetPassengerByPNRNo(PNRNo);
            if (passenger.Status.ToLower().Trim() == "cancelled")
            {
                MessageBox.Show("This ticket is cancelled");
                ResetData();
                return;
            }
            if (passenger.TravelDate <=DateTime .Now )
            {
                MessageBox.Show("The cancelled date of this ticket is greater than now");
                ResetData();
                return;
            }
            cmdCancel.Enabled = true;
            txtFlightNo.Text = passenger.FlightNo;
            txtFName.Text = passenger.FName ;
            txtLName.Text = passenger.LName ;
            txtAge.Text = passenger.Age.ToString ();
            txtGender.Text = passenger.Gender;
            txtTravelDate.Text =passenger.TravelDate.ToString ();
            txtClass.Text = passenger.Class;
            txtStatus.Text = passenger.Status;
            travelclass.SectorID =
            txtAmountPaid.Text = airlineSv.GetFareAmount(txtPnrNo.Text).ToString ();
            txtRefundAmt.Text = travelclass.CalculateRefundAmt(float.Parse(txtAmountPaid.Text), passenger.Status, passenger.TravelDate).ToString ();
        }

        private void ResetData()
        {
            cmdCancel.Enabled = false;
            txtFlightNo.Text = "";
            txtFName.Text = "";
            txtLName.Text ="" ;
            txtAge.Text = "";
            txtGender.Text = "";
            txtTravelDate.Text ="";
            txtClass.Text = "";
            txtStatus.Text = "";
            txtAmountPaid.Text = "";
            txtRefundAmt.Text = "";
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            if (airlineSv != null)
            {
                if (airlineSv.CancelReservation(passenger, float.Parse(txtRefundAmt.Text), travelclass))
                {
                    MessageBox.Show("This ticket is cancelled success");
                    PrintRefund();
                }
            }
        }

        private void PrintRefund()
        {
            PrintRefundTicket frmPrintRefundReceipt = new PrintRefundTicket();
            frmPrintRefundReceipt.strName = txtFName.Text.Trim () + " " + txtLName.Text.Trim ();
            frmPrintRefundReceipt.strPnrNo = txtPnrNo.Text.Trim ();
            frmPrintRefundReceipt.dRefundAmt = decimal.Parse (txtRefundAmt.Text.Trim ());
            frmPrintRefundReceipt.ShowDialog();
        }

        private void frmCancelReservation_Load(object sender, EventArgs e)
        {
            cmdCancel.Enabled = false;
            tsMessage.Text = "This Form use Asbstract Factory and Facade Pattern";
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
